package com.cpi.cornpp.bushelprice.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.bushelprice.dto.BushelPriceDTO;
import com.cpi.cornpp.bushelprice.exception.BushelPriceException;
import com.cpi.cornpp.bushelprice.view.BushelPriceView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.TimeUtils;

/**
 * Bushel Price Converter
 * 
 * @author Edgar Joao
 *
 */
public class BushelPriceConverter {
        /**
         * Bushel Price View To DTO.
         * @author Edgar Joao
         * 
         * @param view
         * @return
         * @throws BushelPriceException
         */
		public BushelPriceDTO converterBushelPriceViewToDto(BushelPriceView view) throws BushelPriceException {
			BushelPriceDTO dto = null;
			try{
			  if(view != null){
				  dto = new BushelPriceDTO();
				  dto.setBushelId(view.getBushelId());
				  dto.setDate(TimeUtils.convertStringToSQLDate(view.getDate()));
				  dto.setMonthPeriodId(view.getMonthPeriodId());
				  dto.setAmount(NumberUtil.parseDouble(view.getAmount()));
				  dto.setUserId(view.getUserId());
				  dto.setInitDate(TimeUtils.convertStringToSQLDate(view.getInitDate()));
				  dto.setEndDate(TimeUtils.convertStringToSQLDate(view.getEndDate()));				  
			  }
			}catch( Exception exception ){
				BushelPriceException bushelPriceException=null;
				exception.printStackTrace(System.out);
				bushelPriceException= new BushelPriceException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
				throw bushelPriceException;
			}
			return dto;
		}
		/**
		 * Bushel Price DTO To View
		 * 
		 * @author Edgar Joao
		 * @param dto
		 * @return
		 * @throws BushelPriceException
		 */
		public BushelPriceView converterBushelPriceDtoToView(BushelPriceDTO dto) throws BushelPriceException{
			BushelPriceView view = null;
			try{
				if(dto != null){
					view = new BushelPriceView();
					view.setBushelId(dto.getBushelId());
					view.setDate(TimeUtils.convertSQLDateToString(dto.getDate()));
					view.setMonthPeriodId(dto.getMonthPeriodId());
					view.setAmount(NumberUtil.convertPrice(dto.getAmount()));					
				}
			}catch( Exception exception ){
				BushelPriceException bushelPriceException=null;
				exception.printStackTrace(System.out);
				bushelPriceException= new BushelPriceException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
				throw bushelPriceException;
			}
			return view;
		}
		/**
		 * Bushel Price DTO List to Views
		 * @author Edgar Joao
		 * @param bushelPriceDTOs
		 * @return
		 * @throws BushelPriceException
		 */
		public List convertDTOsToViews(List bushelPriceDTOs) throws BushelPriceException{
			Iterator iterator     = null;
			List bushelPriceList  = null;
			BushelPriceDTO dto    = null;
			BushelPriceView view  = null;			
			try{
				bushelPriceList = new ArrayList(0);
				iterator = bushelPriceDTOs.iterator();
				while( iterator.hasNext() ){
					dto  = (BushelPriceDTO) iterator.next();
					view = this.converterBushelPriceDtoToView(dto);
					bushelPriceList.add(view);
				}			
			}catch(BushelPriceException bushelPriceException){
				throw bushelPriceException;
			}catch( Exception exception ){
				BushelPriceException bushelPriceException=null;
				exception.printStackTrace(System.out);
				bushelPriceException= new BushelPriceException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
				throw bushelPriceException;
			}			
			return bushelPriceList;
		}
		/**
		 * 
		 * Bushel Price View List to DTO
		 * 
		 * @author Edgar Joao
		 * @param bushelPriceViews
		 * @return
		 * @throws BushelPriceException
		 */
		public List convertViewToDTOs(List bushelPriceViews) throws BushelPriceException{
			Iterator iterator     = null;
			List bushelPriceList  = null;
			BushelPriceDTO dto    = null;
			BushelPriceView view  = null;			
			try{
				bushelPriceList = new ArrayList(0);
				iterator = bushelPriceViews.iterator();
				while( iterator.hasNext() ){
					view  = (BushelPriceView) iterator.next();
					dto = this.converterBushelPriceViewToDto(view);
					bushelPriceList.add(dto);
				}			
			}catch(BushelPriceException bushelPriceException){
				throw bushelPriceException;
			}catch( Exception exception ){
				BushelPriceException bushelPriceException=null;
				exception.printStackTrace(System.out);
				bushelPriceException= new BushelPriceException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
				throw bushelPriceException;
			}			
			return bushelPriceList;
		}
}
